/**
 * 
 */
package com.elite.groovy.callback


import com.elite.groovy.util.EliteBean;
import com.elite.groovy.kh.KHUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.sf.json.JSONObject;

import com.elite.groovy.util.Util

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse;

/**
 * @author Rick
 *
  * @Description //TODO Rick
  * @Date 2022/11/15 15:23
  * @Param
  * @return
  **/

public class autoNetOBTask implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(autoNetOBTask.class);
		private String charset = "utf-8"; //设置字符集
	private int timeout = 100000; //设置请求超时时间
	private int enableSSLflag = 0;	
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

		log.debug("互联网承保回访任务开始生成！");
		//检查中间表数据,每次最多检查100条  --2305025不再每次只检100条
		List synclist = eliteBean.chaxun_sql("select policyno,appliname,applimobile,insuredmobile,insurantname,startdate,starthour,enddate,endhour,comcode,comname,businessplate,businessclass,productcode,productname,goodsname,makecom,businessnature,agentname,companyname,netbusinessflag,clausename,kindname,updatetime,deal_date,appliidentify,insuredtype,syncstatus from net_visit_data where syncstatus=0 and to_char(updatetime,'YYYY-MM-DD')=to_char(SYSDATE,'YYYY-MM-DD') and policy_flag<>'0' ", "dataSource");
		log.debug("互联网承保回访本次数据量："+synclist.size());
		//变量定义
		String Re_Customer_guid;
		if (synclist.size() > 0) {
			//生成任务
			for (int i = 0; i < synclist.size(); i++) {
				//获取同步字段
				String Re_policyno = synclist.get(i).get("policyno");                //保单号
				String Re_appliname = synclist.get(i).get("appliname");                //投保人姓名
				String Re_applimobile = synclist.get(i).get("applimobile");            //投保人电话
				String Re_insuredmobile = synclist.get(i).get("insuredmobile");        //被保人电话
				String Re_insurantname = synclist.get(i).get("insurantname");        //被保人姓名
				String Re_startdate = synclist.get(i).get("startdate");                //起保日期
				String Re_starthour = synclist.get(i).get("starthour");                //起保小时
				String Re_enddate = synclist.get(i).get("enddate");                    //终保日期
				String Re_endhour = synclist.get(i).get("endhour");                    //终保小时
				String Re_comcode = synclist.get(i).get("comcode");                    //归属机构代码
				String Re_comname = synclist.get(i).get("comname");                    //归属机构名称
				String Re_businessplate = synclist.get(i).get("businessplate");        //业务板块
				String Re_businessclass = synclist.get(i).get("businessclass");        //业务分类
				String Re_productcode = synclist.get(i).get("productcode");            //产品代码
				String Re_productname = synclist.get(i).get("productname");            //产品名称
				String Re_goodsname = synclist.get(i).get("goodsname");                //商品名称
				String Re_makecom = synclist.get(i).get("makecom");                    //出单机构
				String Re_businessnature = synclist.get(i).get("businessnature");    //业务来源
				String Re_agentname = synclist.get(i).get("agentname");                //代理人/经纪人
				String Re_companyname = synclist.get(i).get("companyname");            //归属部门
				String Re_netbusinessflag = synclist.get(i).get("netbusinessflag");    //是否为互联网业务
				String Re_clausename = synclist.get(i).get("clausename");            //条款名称
				String Re_kindname = synclist.get(i).get("kindname");                //责任名称
				String Re_updatetime = synclist.get(i).get("updatetime");            //ETL更新时间
				String Re_deal_date = synclist.get(i).get("deal_date");                //
				String Re_insuredtype = synclist.get(i).get("insuredtype");				//个人/团体
				String Re_appliidentify = synclist.get(i).get("appliidentify");			//个人身份证/团体社会信用代码
				
				//通过投保人电话和姓名查找本地客户
				List custlist = eliteBean.chaxun_sql("select customer_guid from customer where mobile='" + Re_applimobile + "' and customername='" + Re_appliname + "' and rownum<=1", "dataSource");
				String Temp_Guid = KHUtil.getRandomString(31).toUpperCase();
				//获取Customer_guid
				if (custlist.size() == 0) {
					Re_Customer_guid = "CUST_" + Temp_Guid;
					eliteBean.update("insert into customer (customer_guid,customername,mobile)values('"+Re_Customer_guid+"','"+Re_appliname+"','"+Re_applimobile+"')", "dataSource");
				} else {
					Re_Customer_guid = custlist.get(0).get("customer_guid");

				}

				//补充客户等级和客户类别
				JSONObject josnbody = new JSONObject();
				josnbody.put("username","LDTP");
				josnbody.put("password","a1e9173fc8090a220c27a33b951f8f7c");
				josnbody.put("policyno",Re_policyno);
				josnbody.put("clientname","");
				josnbody.put("mobile","");
				Map headMap=new HashMap(); //设置头文件
				headMap.put("Content-Type","application/json");
				def url="http://ecif.edhic.com/ecif/clientQueryAnswered/clientFlagToNgs";
				
				def custClass = "";
				def clientType = "";
				try{
					def msgjson = Util.doHttpPostUseHead(url,JSONObject.fromObject(josnbody).toString(),"utf-8",100000,0,headMap);
					log.debug("----客户等级查询数据--------"+Re_policyno);
					JSONObject obj = JSONObject.fromObject(msgjson);
					def code=obj.get("code");
					def resulterror;
					if(code == 1000){
						custClass = obj.get("data").get("custClass");
						clientType = obj.get("data").get("clientType");

						eliteBean.update("update customer set fax = '"+custClass+"', homepage ='"+clientType+"' where customer_guid = '"+Re_Customer_guid+"'","dataSource");
					}
				}catch(Exception e){
					log.debug(e);
				}


				//查找是否同一产品和投保人手机号码是否已生成回访任务
				List OBJCTLIST = eliteBean.chaxun_sql("select Objective_Guid from elite_net_visit_data where applimobile='" + Re_applimobile + "' and productcode='" + Re_productcode + "' and to_char(updatetime,'YYYY-MM-DD')=to_char(SYSDATE,'YYYY-MM-DD') and rownum<=1","dataSource");
				//生成Objective_guid,未查到同批次任务时，直接新增一个
				String Re_Objective_Guid;
				if (OBJCTLIST.size() == 0) {
						Re_Objective_Guid = "OBJE_" + Temp_Guid;
				}else {
						Re_Objective_Guid = OBJCTLIST.get(0).get("Objective_Guid");
				}

				//生成业务表
				eliteBean.update("insert into elite_net_visit_data (objective_guid,policyno,appliname,applimobile,insuredmobile,insurantname,startdate,starthour,enddate,endhour,comcode,comname,businessplate,businessclass,productcode,productname,goodsname,makecom,businessnature,agentname,companyname,netbusinessflag,clausename,kindname,updatetime,deal_date,insuredtype,appliidentify,syncstatus,synctime)select '" + Re_Objective_Guid + "',policyno,appliname,applimobile,insuredmobile,insurantname,startdate,starthour,enddate,endhour,comcode,comname,businessplate,businessclass,productcode,productname,goodsname,makecom,businessnature,agentname,companyname,netbusinessflag,clausename,kindname,updatetime,deal_date,insuredtype,appliidentify,syncstatus,sysdate from net_visit_data where policyno='" + Re_policyno + "' and rownum<=1", "dataSource");
				
				
				//20230525改为全部推机器人回访，且不再生成人工
				if(OBJCTLIST.size() == 0) {
					//20240201改为V5以上客户推人工
					if("VIP五星客户".equals(custClass)||"VIP五星客户-优质".equals(custClass)||"VIP五星客户-关键".equals(custClass)){
						//转人工
						eliteBean.update("insert into ob_visittask (visittask_guid,objective_guid,businessno,map_id,taskstatus,bustype,createddate,modifieddate)VALUES(sys_guid(),'" + Re_Objective_Guid + "','"+Re_policyno+"','YG7JPC',2,'NET',sysdate,sysdate)" , "dataSource");

						//生成Objective
						eliteBean.update("insert into objective (OBJECTIVE_GUID, OBJECTIVETYPE_ID, HANDLEGROUP_ID,PRIORITY_ID,EXPIREDDATE,CUSTOMER_GUID,OBJECTIVESTATUS,CREATEDDATE,CREATEDBY,WORKORDERFRAME_ID,OBJECTIVESUBSTATUS,MAP_ID,CURSTEP_ID_MAP)VALUES('" + Re_Objective_Guid + "','8M6UNW','ORG016','NORMAL',sysdate,'" + Re_Customer_guid + "','OPEN',sysdate,'SELITE','YG7JPC','OPEN','YG7JPC','R41IZC')", "dataSource");

						//生成EliteTask
						eliteBean.update("insert into elitetask (ELITETASK_GUID, OBJECTIVE_GUID, OBJECTIVETYPE_ID, EXPECTSTARTTIME,OBSTATUS,PRIORITY, HANDLEGROUP_ID, CREATEDBY, CREATEDDATE,CUSTOMER_GUID,CURSTEPSTATUS,CURSTEP_ID_MAP,MAP_ID,OBJECTIVESUBSTATUS,ELITETASKSTATUS, OBFLAG) select sysguid,OBJECTIVE_GUID,OBJECTIVETYPE_ID,EXPECTSTARTTIME,OBSTATUS,'NORMAL',HANDLEGROUP_ID,CREATEDBY,CREATEDDATE,CUSTOMER_GUID,'OPEN',CURSTEP_ID_MAP,MAP_ID,'OPEN','OPEN','1' FROM OBJECTIVE where OBJECTIVE_GUID='" + Re_Objective_Guid + "'", "dataSource");
					}else{
						eliteBean.update("insert into ob_visittask (visittask_guid,objective_guid,businessno,map_id,taskstatus,bustype,createddate,modifieddate)VALUES(sys_guid(),'" + Re_Objective_Guid + "','"+Re_policyno+"','YG7JPC',0,'NET',sysdate,sysdate)" , "dataSource");
					}
				}else{
					//同一产品和投保人手机号码再次出现, 则将原ob_visittask标注为 4-同批次多次投保，为推送MQ时拼接被保人做标识。
					//多次投保打标识
					eliteBean.update("update ob_visittask set taskstatus=4 where objective_guid='" + Re_Objective_Guid + "' and taskstatus=0 ", "dataSource");
				}

				//变更同步表数据
				eliteBean.update("update net_visit_data set syncstatus=1 where policyno='" + Re_policyno + "'", "dataSource");
			}
		} else {
			log.debug("未查询到互联网承保回访任务数据！");
		}

return "1";
	}

}

